home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / csr_001.arc / TIMERS.C < prev   
Text File  |  1988-09-02  |  4KB  |  205 lines

  1. /* timers.c
  2.  *
  3.  *    Return elapsed time (in tenths of seconds)
  4.  *    since kick-off of timer.
  5.  *
  6.  *    D. Perras    03 Jan 1987
  7.  *  Datalight C
  8.  *
  9.  *      B. Pritchett    05 Jan 1987
  10.  *  Microsoft C Modifications
  11.  *
  12.  *    Routines included:
  13.  *
  14.  *        void    init_tmr()        Initialize the timer for operation
  15.  *        void    start_tmr()        Start the timer counting
  16.  *    unsigned    stop_tmr()        Stop the timer, return results
  17.  *    unsigned    read_tmr()        Read the elapsed time since start_tmr()
  18.  *        void    reset_tmr()        Reset the timer does not return elapsed time
  19.  *        void    zero_tmr()        Zero the timer (relative), keep it running
  20.  *
  21.  *         long     get_timer()        Return the PC's current timer (internal)
  22.  */
  23.  
  24. #include <dos.h>  /* This is to include the REGS structure. */
  25.                   /* It can be changed as needed for the compiler */
  26.  
  27.  
  28. #define    MSC    1        /* references union: regs.x.cx vs. regs.cx */
  29.  
  30. long    timers[10], t_dvsr;
  31. int    timer_flag[10];
  32.  
  33. #define     INIT_FLG    1
  34. #define     STRT_FLG    2
  35. #define     TMR_ON(j)        (timer_flag[(j)] & STRT_FLG)
  36. #define     TMR_INIT(j)    (timer_flag[(j)] & INIT_FLG)
  37. #define  TIMER_INT    0x1A
  38.  
  39. #define     ERRVAL        0
  40.  
  41. unsigned init_tmr()
  42. {
  43. /*    This routine does two timer calls and compares the return values
  44.  *  just to be sure the timer is working.
  45.  *    Return ERRVAL if error found, otherwise returns (unsigned) tm1
  46.  */
  47.  
  48.     int i;
  49.     unsigned tm1;
  50.     long tmv, get_timer();
  51.     
  52.     for (i=0; i<10; i++) 
  53.         timer_flag[i] = timers[i] = 0;
  54.     tmv = get_timer();
  55.  
  56.     for (i=0; i<16000; i++)
  57.         ;                    /* Null 'for' loop to use up some time */
  58.     tm1 = get_timer() - tmv;
  59.  
  60. /* Currently, we test the result value for non-zero.  However, if the
  61.  * system environment demands it, a value range could be tested and the
  62.  * t_dvsr variable assigned accordingly.  The 182 value is for IBM PC's
  63.  * and compatibles.
  64.  */
  65.  
  66.     if (tm1 != 0)
  67.         for (i=0; i<10; i++)    
  68.             timer_flag[i] |= INIT_FLG;
  69.     else tm1 = ERRVAL;
  70.     t_dvsr = 182;
  71.     return tm1;
  72. }
  73.  
  74. void start_tmr(x)
  75. int x;
  76. {
  77. /*    This routine picks up the current time stored by the timer
  78.  *    and puts it in timers[x].
  79.  *    It also set the 'timer started indicator'
  80.  *    It returns nothing.
  81.  */
  82.  
  83.     long get_timer();
  84.  
  85.     x = abs(x)%10;
  86.  
  87.     if (TMR_INIT(x) && !TMR_ON(x)) {
  88.         timers[x] = get_timer();
  89.         timer_flag[x] |= STRT_FLG;
  90.     }
  91. }
  92.  
  93. unsigned stop_tmr(x)
  94. int x;
  95. {
  96. /*    This routine stops the timer and returns the difference
  97.  *    between the count stored by the start_tmr and the timer
  98.  *    count at the point of stopping.
  99.  *  The difference is made significant to the tenths-of-seconds.
  100.  */
  101.  
  102.     unsigned tmp;
  103.     long    tmp1, get_timer();
  104.  
  105.     x = abs(x)%10;
  106.  
  107.     if (TMR_ON(x)) {
  108.         tmp1 = get_timer()-timers[x];
  109.         timer_flag[x] &= ~STRT_FLG;
  110.         tmp1 *= 100;
  111.         tmp1 /=  t_dvsr;
  112.         timers[x] = 0;
  113.         tmp = tmp1;
  114.         return tmp;
  115.     } else return ERRVAL;
  116. }
  117.  
  118. unsigned read_tmr(x)
  119. int x;
  120. {
  121. /*    This routine works line stop_tmr except that the timer is
  122.  *    is not stopped.  The elapsed time is returned and the timer
  123.  *    is continued.
  124.  *  Successive calls to read_tmr will return successivelly greater values.
  125.  */
  126.  
  127.     unsigned tmp;
  128.     long    tmp3, get_timer();
  129.  
  130.     x = abs(x)%10;
  131.  
  132.     if (TMR_ON(x)) {
  133.         tmp3 = get_timer()-timers[x];
  134.         tmp3 *= 100;
  135.         tmp3 /= t_dvsr;
  136.         tmp = tmp3;
  137.         return tmp;
  138.     } else return ERRVAL;
  139. }
  140.  
  141. void reset_tmr(x)
  142. int x;
  143. {
  144. /* This function resets the specified timer to 0
  145.  * and clears the TMR_ON flag.  It does not return
  146.  * a value.
  147.  */
  148.  
  149.     x = abs(x)%10;
  150.  
  151.     if (TMR_ON(x)) {
  152.         timers[x] = 0;
  153.         timer_flag[x] &= ~STRT_FLG;
  154.     }
  155. }
  156.  
  157. void zero_tmr(x)
  158. int x;
  159. {
  160. /*  This function zeros the specified timer relative to the time it
  161.  *  is called.  The timer operation is continued.
  162.  *  It does not return a timer value.
  163.  */
  164.  
  165.     long get_timer();
  166.  
  167.  
  168.     x = abs(x)%10;
  169.  
  170.     if (TMR_ON(x)) {
  171.         timers[x] = get_timer();
  172.     }
  173. }
  174.  
  175.  
  176. long get_timer()
  177. {
  178. /*    This routine returns the value in the PC's timer
  179.  *    It is placed here so that the int86 call can be
  180.  *  changed, if needed, without effecting the code above.
  181.  */
  182.  
  183.  
  184.     static long    tmp1;
  185. #ifdef MSC
  186.     static union REGS regs;
  187. #else
  188.     static REGS regs;
  189. #endif
  190.     static int flags;
  191.  
  192. #ifdef MSC
  193.         regs.x.ax = 0;
  194.         flags = int86(TIMER_INT,®s,®s);
  195.         tmp1 = regs.x.cx * 65536 + regs.x.dx;
  196. #else
  197.         regs.ax = 0;
  198.         flags = int86(TIMER_INT,®s,®s);
  199.         tmp1 = regs.cx  * 65536 + regs.dx;
  200. #endif
  201.         return(tmp1);
  202. }
  203.  
  204. /* end of timers.c    */
  205.